package it.tcm.TeacherClassMate.util;


import it.tcm.TeacherClassMate.beans.MailBean;
import it.tcm.TeacherClassMate.facade.MailManageBase;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import org.apache.log4j.Logger;

import com.sun.mail.smtp.SMTPMessage;

public class SendMail {
	
private static Logger log = Logger.getLogger("it.tcm.TeacherClassMate.util.sendMail".getClass());
	
	public static void sendMail(String email, String subject, String body) throws NamingException, MessagingException {
		log.info("send mail");	
		Context initial = new InitialContext();
		Object o =  initial.lookup("java:comp/env/mail/Session");
		Session session = (Session)o;
        MimeMessage message = new MimeMessage(session);
        message.setSubject(subject);
        message.setRecipients(javax.mail.Message.RecipientType.TO, javax.mail.internet.InternetAddress.parse(email, false));        
        message.setText(body);       
        Transport.send(message);
        log.info("sent mail ");
    }
	
	public static void sendMailAndUpdate(MailBean mail) throws NamingException, MessagingException {
		log.info("send mail and update");	
		Context initial = new InitialContext();
		Object o =  initial.lookup("java:comp/env/mail/Session");
		Session session = (Session)o;
        MimeMessage message = new MimeMessage(session);
        message.setSubject(mail.getObject());
        message.setRecipients(javax.mail.Message.RecipientType.TO, javax.mail.internet.InternetAddress.parse(mail.getDest(), false));        
        message.setText(mail.getBody());
        SMTPMessage smtpMessage = new SMTPMessage(message);
        smtpMessage.setHeader("Sender", "login@verificafast.hostjava.net");
        smtpMessage.setHeader("signed-by", "login@verificafast.hostjava.net");
        smtpMessage.setHeader("mailed-by", "login@verificafast.hostjava.net");
        smtpMessage.setFrom(new InternetAddress("login@verificafast.hostjava.net"));
        smtpMessage.setEnvelopeFrom("login@verificafast.hostjava.net");
        Transport.send(smtpMessage);
        //Transport.send(message);
        mail.setSent("1");
        MailManageBase mmb = new MailManageBase();
        mmb.save(mail);
        log.info("sent mail ");
    }
	
	
	public static void sendMailWithAttach(String email, String subject, String body, String filename, String file) throws NamingException, MessagingException {
		log.info("send mail");	
		Context initial = new InitialContext();
		Object o =  initial.lookup("java:comp/env/mail/Session");
		Session session = (Session)o;
        MimeMessage message = new MimeMessage(session);
        message.setSubject(subject);
        message.setRecipients(javax.mail.Message.RecipientType.TO, javax.mail.internet.InternetAddress.parse(email, false));
        MimeBodyPart messagePart = new MimeBodyPart();
        messagePart.setText(body);

    	MimeBodyPart attachmentPart = new MimeBodyPart();
    	FileDataSource fileDataSource = new FileDataSource(file) {
    		public String getContentType() {
    				return "application/octet-stream";
    		}
    	};
    	attachmentPart.setDataHandler(new DataHandler(fileDataSource));
    	attachmentPart.setFileName(filename); 
    	Multipart multipart = new MimeMultipart();
    	multipart.addBodyPart(messagePart);
    	multipart.addBodyPart(attachmentPart); 
    	message.setContent(multipart); 
    	Transport.send(message);
        log.info("sent mail ");
    }
	
	public static void sendMailWithAttachAndUpdate(MailBean mail) throws NamingException, MessagingException {
		log.info("send mail");	
		Context initial = new InitialContext();
		Object o =  initial.lookup("java:comp/env/mail/Session");
		Session session = (Session)o;
        MimeMessage message = new MimeMessage(session);
        message.setSubject(mail.getObject());
        message.setRecipients(javax.mail.Message.RecipientType.TO, javax.mail.internet.InternetAddress.parse(mail.getDest(), false));
        MimeBodyPart messagePart = new MimeBodyPart();
        messagePart.setText(mail.getBody());
    	MimeBodyPart attachmentPart = new MimeBodyPart();
    	
    	FileDataSource fileDataSource = new FileDataSource(mail.getFile()) {
    		public String getContentType() {
    				return "application/octet-stream";
    		}
    	};
    	attachmentPart.setDataHandler(new DataHandler(fileDataSource));
    	attachmentPart.setFileName(mail.getFilename()); 
    	Multipart multipart = new MimeMultipart();
    	multipart.addBodyPart(messagePart);
    	multipart.addBodyPart(attachmentPart); 
    	message.setContent(multipart); 
    	SMTPMessage smtpMessage = new SMTPMessage(message);
    	smtpMessage.setHeader("Sender", "verifica@verificafast.hostjava.net");
        smtpMessage.setHeader("signed-by", "verifica@verificafast.hostjava.net");
        smtpMessage.setHeader("mailed-by", "verifica@verificafast.hostjava.net");
        smtpMessage.setFrom(new InternetAddress("verifica@verificafast.hostjava.net"));
        smtpMessage.setEnvelopeFrom("verifica@verificafast.hostjava.net");
    	Transport.send(smtpMessage);
    	//Transport.send(message);
        log.info("sent mail ");
    	mail.setSent("1");
        MailManageBase mmb = new MailManageBase();
        mmb.save(mail);	
    }
	
	
	
}
